﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace VitCode.Web.BackStageManagement
{
    public partial class MenuUpdate : VcPage
    {
        CastleAccess acc = new CastleAccess();
        readonly BaseDataManager help = new BaseDataManager();
        private ObjXmlReader m_XmlReader;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LabelId.Text = publ.GetUrlToReceive(Request.QueryString["id"].Trim());
                string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "BackStageManagement\\PermissionEnum.xml";
                m_XmlReader = new ObjXmlReader(xmlPath);
                XmlNode node = m_XmlReader.GetXmlNode("Permission/Operate");
                var nodeList = m_XmlReader.GetNodeList("Permission/Operate");
                if (nodeList == null || nodeList.Count <= 0) return;
                for (var i = 0; i < nodeList.Count; i++)
                {
                    string strText = m_XmlReader.GetNodeAttributeValue(nodeList[i], "Text", true);
                    string strValue = m_XmlReader.GetNodeAttributeValue(nodeList[i], "Value", true);
                    var item = new ListItem { Text = strText, Value = strValue };
                    CheckListOperate.Items.Add(item);
                }
                if (!string.IsNullOrEmpty(LabelId.Text.Trim()))
                {
                    VitCode.Castle.Entities.Menu objMenu;
                    IList lstObj = acc.GetObjectList(new Menu().GetType().Name, "Id", LabelId.Text.Trim());
                    if (lstObj != null && lstObj.Count > 0)
                    {
                        objMenu = (VitCode.Castle.Entities.Menu)(lstObj[0]);

                        ZLTextBox_MenuName.Text = objMenu.Name;
                        ZLTextBox_FileName.Text = objMenu.UrlPath;
                        ZLTextBox_Memo.Text = objMenu.Memo;
                        ZLTextBox_SortKey.Text = objMenu.SortKey;
                        ZLTextBox_KeyWord.Text = objMenu.KeyWord;

                    }
                }
            }
        }
        
 
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            VitCode.Castle.Entities.Menu objMenu;
            string strName = ZLTextBox_MenuName.Text.Trim();
            string strUrlPath = ZLTextBox_FileName.Text.Trim();
            string strMemo = ZLTextBox_Memo.Text.Trim();
            string strSorkKey = ZLTextBox_SortKey.Text.Trim();
            string strKeyWord = ZLTextBox_KeyWord.Text.Trim();
            string strCurrentId = LabelId.Text.Trim();
            string strOperateList = "";
            foreach (ListItem item in CheckListOperate.Items)
            {
                strOperateList += item.Value.Trim() + ";";
               
            }
            if(strOperateList.Length > 0)
            {
                strOperateList = strOperateList.Substring(0, strOperateList.Length - 1);
            }
            IList lstObj = acc.GetObjectList(new Menu().GetType().Name, "Id", strCurrentId);
            if (lstObj != null && lstObj.Count > 0)
            {
                objMenu = (VitCode.Castle.Entities.Menu)(lstObj[0]);
                objMenu.Name = strName;
                objMenu.UrlPath = strUrlPath;
                objMenu.Memo = strMemo;
                objMenu.KeyWord = strKeyWord;
                objMenu.SortKey = strSorkKey;
                objMenu.Operate = strOperateList;
                bool access = help.SaveOrUpdateObj(objMenu);
                if (access)
                {
                    Alert.MessageBox("修改成功!");
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MenuList.aspx", false);
        }
    }
}
